<template>
	<view class="indexClass">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" title="首页" fixed="true" />
		<u-notice-bar text="这里是公告栏" mode="link" url="/pages/myNotice/myNotice" link-type="switchTab" color="#00aded"></u-notice-bar>
		<view class="indexTop"></view>
		<view class="indexMain" v-if="!roleCodes.includes('siji') && flag">
			<uni-section :title="item.menuLabel" :key="item.menuId" type="line" titleColor="#00aded" class="indexMain_section" v-for="(item, index) in menuList" padding>
				<uni-grid :column="3" :showBorder="false" square @change="changeGrid($event, item.children)">
					<uni-grid-item :index="cindex" :key="cindex" v-for="(citem, cindex) in item.children">
						<view class="grid-item-box" style="background-color: #fff">
							<uni-badge class="uni-badge-left-margin" :text="citem.badgeText" absolute="rightTop" :offset="[-3, -3]" size="small">
								<uni-icons type="image" :size="30" v-if="citem.menuIcon == null" color="#00aded"></uni-icons>
								<uni-icons custom-prefix="iconfont" :type="citem.menuIcon" :size="30" color="#00aded" v-else-if="citem.menuIcon.includes('wl')" />
								<uni-icons type="image" :size="30" color="#00aded" v-else></uni-icons>
								<text class="text">{{ citem.menuLabel }}</text>
							</uni-badge>
						</view>
					</uni-grid-item>
				</uni-grid>
			</uni-section>
		</view>
		<view class="indexFloot" v-else>
			<driverIndex :key="driverTime"></driverIndex>
		</view>
	</view>
</template>

<script>
import { getMenu } from '@/utils/getMenu.js';
import driverIndex from '@/pages/index/driverIndex.vue';
export default {
	components: {
		driverIndex
	},
	data() {
		return {
			flag: true,
			menuList: [],
			roleCodes: [],
			driverTime: null
		};
	},
	onShow() {
		//获取权限数据
		this.roleCodes = [];
		let roleCodes = JSON.parse(uni.getStorageSync('roleCodes'));
		let userName = uni.getStorageSync('userName');
		this.roleCodes = roleCodes;
		if (roleCodes.includes('siji')) {
			this.driverTime = new Date().getTime() + '';
		} else {
			// 获取目录数据
			getMenu({
				type: '首页'
			}).then((res) => {
				this.menuList = res;
				try {
					//查询计时复报待处理数据
					this.menuList.forEach((item) => {
						if (item.children && item.children.length > 0) {
							item.children.forEach(async (citem) => {
								if (citem.menuLabel == '复报') {
									const res = await this.$myRequest({
										url: '/api/v1/tms/getTransOrderList?apiId=627',
										data: { approveStatus: '拒绝', workFlag: '计时' }
									}).catch((err) => {
										uni.showToast({
											duration: 2000,
											title: err,
											icon: 'none'
										});
									});
									citem.badgeText = res.data.data.total;
									this.flag = false;
									this.menuList = JSON.parse(JSON.stringify(this.menuList));
									this.flag = true;
								}
							});
						}
					});
				} catch (e) {
					//TODO handle the exception
				}
			});
		}
	},
	methods: {
		sectionClickRight() {},
		//监听点击网格事件
		async changeGrid(e, gridList) {
			let gridItem = gridList[e.detail.index];
			if (gridItem.menuUrl) {
				uni.setStorageSync('activeMenu', gridItem.menuId);
				let appId = uni.getStorageSync('appId');
				let companyId = uni.getStorageSync('companyId');
				const res = await this.$mySsoRequest({
					url:
						'/icore.icp.web/pass/v1/sysmenus/findFunctionMenusByUserId?companyId=' +
						companyId +
						'&appId=' +
						appId +
						'&userId=' +
						uni.getStorageSync('userId') +
						'&menuType=2' +
						'&pId=' +
						gridItem.menuId
				}).catch((e) => {
					uni.showToast({
						duration: 2000,
						title: e.message,
						icon: 'none'
					});
				});
				let arr = res.data.data
					.filter((item) => {
						return item.pId == gridItem.menuId;
					})
					.map((item) => {
						return item.privilege;
					});
				uni.setStorageSync('preHiddenData', JSON.stringify(arr));
				uni.navigateTo({
					url: gridItem.menuUrl
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.indexClass {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	.indexTop {
		width: 100%;
		height: 100%;
		&_section {
			margin-top: 10rpx;
			min-height: 200rpx;
			width: 100%;
			height: 100%;
			&_content {
			}
		}
	}
	.indexMain {
		&_section {
			margin-top: 10rpx;
			min-height: 240rpx;
			.grid-item-box {
				flex: 1;
				// position: relative;
				/* #ifndef APP-NVUE */
				display: flex;
				/* #endif */
				flex-direction: column;
				align-items: center;
				justify-content: center;
				// padding: 15px 0;
				.uni-badge-left-margin {
					flex: none;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
	.indexFloot {
		margin-top: 20rpx;
	}
}
</style>
